#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QDebug>
#include <QRect>
#include "fullscrstructedlight.h"

FullscrStructedLight::FullscrStructedLight(QWidget *parent) :
    QWidget(parent)
{
    myWidth = parent->width();
    myHeight = parent->height();
    colorList = NULL;
    widthList = NULL;
    sLightStyle = HSTRIP;
}

FullscrStructedLight::~FullscrStructedLight()
{

}

void FullscrStructedLight::endThis()
{
    this->close();
}

void FullscrStructedLight::keyPressEvent(QKeyEvent *keyEvent)
{
    if (keyEvent->key()==Qt::Key_Escape)
    {
        this->close();
    }
}

void FullscrStructedLight::setColorList(QList<QColor> *newColorList)
{
    colorList = newColorList;
}

void FullscrStructedLight::setWidthList(QList<int> *newWidthList)
{
    widthList = newWidthList;
}

void FullscrStructedLight::setSLightStye(LightStyle styleIndex)
{
    sLightStyle = styleIndex;
#ifdef DEBUG
    qDebug() << "Structed Light Style:" << sLightStyle;
#endif
}

void FullscrStructedLight::paintEvent(QPaintEvent *pEvent)
{
#ifdef DEBUG
//    qDebug() << "paintEvent:";
#endif
//    drawHStrips();
    switch (sLightStyle) {
    case VSTRIP: {drawVStrips(); break;}
    case HSTRIP: {drawHStrips(); break;}
    }
}

void FullscrStructedLight::drawVStrips()
{
    QPainter painter(this);
    QBrush brush;
    QPen pen;

    int countWidth = 0;
    int curWidth = 0;
    int colorNum = colorList->count();
    while (countWidth < myWidth) {
        for (int i=0; i<colorNum; i++) {
            if (countWidth >= myWidth) break;
            pen.setColor(colorList->at(i));
            brush.setColor(colorList->at(i));
            brush.setStyle(Qt::SolidPattern);
            curWidth = widthList->at(i);
            QRect newRect(countWidth, 0, curWidth, myHeight);

            painter.setPen(pen);
            painter.setBrush(brush);
            painter.drawRect(newRect);
#ifdef DEBUG
//            qDebug() << "Draw Rect:" << newRect
//                    << "with color:" << colorList->at(i);
#endif
            countWidth += curWidth;
        }
    }
}

void FullscrStructedLight::drawHStrips()
{
    QPainter painter(this);
    QBrush brush;
    QPen pen;

    int countWidth = 0;
    int curWidth = 0;
    int colorNum = colorList->count();
    while (countWidth < myHeight) {
        for (int i=0; i<colorNum; i++) {
            if (countWidth >= myWidth) break;
            pen.setColor(colorList->at(i));
            brush.setColor(colorList->at(i));
            brush.setStyle(Qt::SolidPattern);
            curWidth = widthList->at(i);
            QRect newRect(0, countWidth, myWidth, curWidth);

            painter.setPen(pen);
            painter.setBrush(brush);
            painter.drawRect(newRect);
//#ifdef DEBUG
//            qDebug() << "Draw Rect:" << newRect
//                    << "with color:" << colorList->at(i);
//#endif
            countWidth += curWidth;
        }
    }
}
